using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;

[BurstCompile]
public struct MapDataInitJob : IJobParallelForBatch
{
    public int2 MapSize;
    public float GridSize;

    [WriteOnly]
    public NativeArray<int2> GridIndexPairs;

    [WriteOnly]
    public NativeArray<float2> GridPositions;

    [BurstDiscard]
    public void WrapData(MapData mapData)
    {
        this.MapSize = new int2(mapData.Width, mapData.Height);
        this.GridSize = mapData.GridSize;
        this.GridIndexPairs = mapData.GridIndex2Ds;
        this.GridPositions = mapData.GridPositions;
    }

    [BurstCompile]
    public void Execute(int startIndex, int count)
    {
        var positionOffset = (float2)(MapSize.xy - 1) * GridSize * -0.5f;

        for (int i = startIndex; i < startIndex + count; i++)
        {
            var indexX = i / MapSize.y;
            var indexY = i % MapSize.y;
            GridIndexPairs[i] = new int2(indexX, indexY);
            GridPositions[i] = new float2(indexX * GridSize, indexY * GridSize) + positionOffset;
        }
    }
}
